﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.LoaiTheVip
{
    public partial class frmXuLy : Form
    {
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        #endregion

        #region khởi tạo
        public GetString GetData;
        bool checkClose = false;
        bool kiemtra = false;
        Entities.LoaiTheVip temp;
        Entities.LoaiTheVip loaithevip;

        public frmXuLy()
        {
            InitializeComponent();
            txtMaLoaiTheVip.Text = getID();
        }

        public frmXuLy(Entities.LoaiTheVip temp)
        {
            InitializeComponent();
            kiemtra = true;
            this.temp = temp;
            XuLyDuLieu(temp);
        }
        #endregion

        #region Ghi lại
        public void GhiLai()
        {
            if (kiemtra)
                Sua();
            else
                Them();
        }

        #region thêm
        void Them()
        {
            loaithevip = XuLyObject(Common.Constants.Insert);
            // check object
            if (loaithevip == null)
            {
                return;
            }
            // check conflic insert
            if (!CheckConflicInsert(loaithevip))
            {
                MessageBox.Show(Common.MSG.MSG5(loaithevip.MaLoaiTheVip), Common.MSG.TieuDe, MessageBoxButtons.OK);
                GetData(true);
                txtMaLoaiTheVip.Text = maloaithevip;
                return;
            }
            // check insert
            loaithevip.TaiKhoan = UtilitiesVariable.User;
            if (Common.StaticVariable.InterfaceObject.InsertUpdateLoaiTheVip(loaithevip) == 0)
            {
                return;
            }
            checkClose = true;
            this.Close();
        }
        #endregion

        #region Check Conflic Insert
        string maloaithevip;
        public bool CheckConflicInsert(Entities.LoaiTheVip loaithevip)
        {
            try
            {
                maloaithevip = getID();
                if (loaithevip.MaLoaiTheVip == maloaithevip)
                {
                    return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region Sửa
        void Sua()
        {
            try
            {
                loaithevip = XuLyObject(Common.Constants.Update);
                // check object
                if (loaithevip == null)
                {
                    return;
                }
                // check conflic update
                if (!CheckConflicUpdate())
                {
                    MessageBox.Show(Common.MSG.MSG6(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check update
                loaithevip.TaiKhoan = UtilitiesVariable.User;
                if (Common.StaticVariable.InterfaceObject.InsertUpdateLoaiTheVip(loaithevip) == 0)
                {
                    return;
                }
                checkClose = true;
                this.Close();

            }
            catch
            {
            }
        }
        #endregion

        #region Check Conflic Update
        bool CheckConflicUpdate()
        {
            try
            {
                Entities.LoaiTheVip[] mangchitiet = Common.StaticVariable.InterfaceObject.SelectLoaiTheVip("1");
                for (int i = 0; i < mangchitiet.Length; i++)
                {
                    if (mangchitiet[i].MaLoaiTheVip == temp.MaLoaiTheVip)
                    {
                        return Check(mangchitiet[i]);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Check(Entities.LoaiTheVip loaithevip)
        {
            try
            {
                bool kt = true;
                if (temp.MaLoaiTheVip != loaithevip.MaLoaiTheVip)
                {
                    txtMaLoaiTheVip.Text = temp.MaLoaiTheVip = loaithevip.MaLoaiTheVip;
                    kt = false;
                }
                if (temp.GhiChu != loaithevip.GhiChu)
                {
                    txtGhiChu.Text = temp.GhiChu = loaithevip.GhiChu;
                    kt = false;
                }
                if (temp.GiaTriThe != loaithevip.GiaTriThe)
                {
                    txtGiaTriThe.Text = temp.GiaTriThe = loaithevip.GiaTriThe;
                    kt = false;
                }
                if (temp.TenLoaiThe != loaithevip.TenLoaiThe)
                {
                    txtTenLoaiTheVip.Text = temp.TenLoaiThe = loaithevip.TenLoaiThe;
                    kt = false;
                }
                if (temp.SoDiemThuong != loaithevip.SoDiemThuong)
                {
                    txtSoDiemThuong.Text = temp.SoDiemThuong = loaithevip.SoDiemThuong;
                    kt = false;
                }
                return kt;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #endregion

        #region thoát
        public void Thoat()
        {
            this.Close();
        }
        #endregion

        #region getid
        public string getID()
        {
            try
            {
                string str = "";
                Entities.GetID layid = new Entities.GetID();
                layid.TableName = "LoaiTheVip";
                Entities.GetID lay = Common.StaticVariable.InterfaceObject.SelectID(layid);
                if (lay.ColumnName == null)
                {
                    return "LTV_0001";
                }
                str = Common.Utilities.ProcessID(lay.ColumnName);
                return str;
            }
            catch
            {
                return "LTV_0001";
            }
        }
        #endregion

        #region xử lý object

        public void XuLyDuLieu(Entities.LoaiTheVip loaithevip)
        {
            try
            {
                txtGhiChu.Text = loaithevip.GhiChu;
                txtMaLoaiTheVip.Text = loaithevip.MaLoaiTheVip;
                txtTenLoaiTheVip.Text = loaithevip.TenLoaiThe;
                txtSoDiemThuong.Text = loaithevip.SoDiemThuong;
                txtGiaTriThe.Text = loaithevip.GiaTriThe;
            }
            catch
            {
            }
        }

        public bool Validate()
        {
            try
            {
                if (txtTenLoaiTheVip.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbTenLoaiThe.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    txtTenLoaiTheVip.Focus();
                    return false;
                }
                if (txtGiaTriThe.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbGiaTriThe.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    txtGiaTriThe.Focus();
                    return false;
                }
                if (float.Parse(txtGiaTriThe.Text) <= 0)
                {
                    MessageBox.Show(Common.MSG.MSG22(lbGiaTriThe.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    txtGiaTriThe.Focus();
                    return false;
                }
                if (txtSoDiemThuong.Text == "")
                {
                    MessageBox.Show(Common.MSG.MSG13(lbSoDiemThuong.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    txtSoDiemThuong.Focus();
                    return false;
                }
                if (float.Parse(txtSoDiemThuong.Text) <= 0)
                {
                    MessageBox.Show(Common.MSG.MSG22(lbSoDiemThuong.Text), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    txtSoDiemThuong.Focus();
                    return false;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public Entities.LoaiTheVip XuLyObject(string hanhDong)
        {
            try
            {
                if (Validate())
                {
                    loaithevip = new Entities.LoaiTheVip();
                    loaithevip.HanhDong = hanhDong;
                    loaithevip.GhiChu = txtGhiChu.Text;
                    loaithevip.MaLoaiTheVip = txtMaLoaiTheVip.Text;
                    loaithevip.TenLoaiThe = txtTenLoaiTheVip.Text;
                    loaithevip.SoDiemThuong = txtSoDiemThuong.Text;
                    loaithevip.GiaTriThe = txtGiaTriThe.Text;
                    return loaithevip;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
        #endregion

        #region event
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            btnGhiLai.Click += new EventHandler(btnGhiLai_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            txtGiaTriThe.TextChanged += new EventHandler(txtGiaTriThe_TextChanged);
            txtSoDiemThuong.TextChanged += new EventHandler(txtSoDiemThuong_TextChanged);
            this.FormClosing += new FormClosingEventHandler(frmXuLy_FormClosing);
        }

        void txtSoDiemThuong_TextChanged(object sender, EventArgs e)
        {
            TextBox temp = (TextBox)sender;
            int vt = temp.SelectionStart;
            string str = new string(temp.Text.ToList<char>().Where(c => char.IsNumber(c)).ToArray<char>());

            temp.Text = str;

            temp.SelectionStart = vt;
        }

        void txtGiaTriThe_TextChanged(object sender, EventArgs e)
        {
            TextBox temp = (TextBox)sender;
            int vt = temp.SelectionStart;
            string str = new string(temp.Text.ToList<char>().Where(c => char.IsNumber(c)).ToArray<char>());

            temp.Text = str;

            temp.SelectionStart = vt;
        }

        void frmXuLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
            else
            {
                GetData(kiemtra);
            }
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void btnGhiLai_Click(object sender, EventArgs e)
        {
            GhiLai();
        }
        #endregion
    }
}
#endregion